<?php
/**
 *
 * (model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\RakutenAccount;
use App\Models\Basics\ShopifyAccount;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlat;
use Illuminate\Database\Eloquent\Model;
class DailySa extends Model
{
    protected $table = 'sr_daily_sa';

    protected $guarded = [];

    public $timestamps = false;

    public function amazon_account(){
        return $this->belongsTo(AmazonAccount::class,'account_id');
    }
    public function shopify_account(){
        return $this->belongsTo(ShopifyAccount::class,'account_id');
    }
    public function rakuten_account(){
        return $this->belongsTo(RakutenAccount::class,'account_id');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function plat(){
        return $this->belongsTo(SysPlat::class,'plat_id','plat_id');
    }

    public static function headersField(){
        $fields = [];
        // $fields['account'] = ['title' => '账号','sumField' => false];
        // $fields['area'] = ['title' => '站点','sumField' => false];
        // $fields['country'] = ['title' => '国家','sumField' => false];
        // for($i = self::V_NUM; $i >= 1; $i--){
        //     $fields['day'.$i] = [
        //         'title' => date("m.d",strtotime("-$i days")),
        //         'sumField' => true
        //     ];
        // }
        $fields['avg3'] = ['title' => '3天AVG','sumField' => true];
        $fields['avg7'] = ['title' => '7天AVG','sumField' => true];
        $fields['avg15'] = ['title' => '15天AVG','sumField' => true];
        $fields['avg30'] = ['title' => '30天AVG','sumField' => true];
        // $fields['avg'] = ['title' => 'AVG','sumField' => false];
        // $fields['sales_status'] = ['title' => '销量趋势','sumField' => false];
        return $fields;
    }

    /**
     * 获取平均销售额的汇总
     * @param null $platId
     * @param null $accountId
     * @param null $countryId
     * @param null $areaId
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function getAvgDaySum($platId = null, $accountId = null, $countryId = null, $areaId = null){
        $query = self::query()
            ->selectRaw('sum(avg3) as avg3,
            sum(avg7) as avg7,
            sum(avg15) as avg15,
            sum(avg30) as avg30,
            sum(avg) as avg');
        if ($platId) $query->where('plat_id', $platId);
        if ($accountId) $query->where('account_id', $accountId);
        if ($countryId) $query->where('country_id', $countryId);
        if ($areaId) $query->where('area_id', $areaId);
        return $query->first();
    }

    public static function getDownloadData($re, $days, $startTime, $endTime, $timeMax){
        foreach($re as $item) {
            $temp = [];
            $temp['account'] = '';
            if ($item->plat_id == SysPlat::PLAT_AMAZON) {
                $temp['account'] = $item->amazon_account->account ?? '';
            }
            if ($item->plat_id == SysPlat::PLAT_SHOPIFY) {
                $temp['account'] = $item->shopify_account->account ?? '';
            }
            if ($item->plat_id == SysPlat::PLAT_RAKUTEN) {
                $temp['account'] = $item->rakuten_account->account ?? '';
            }

            $temp['plat'] = $item->plat->title ?? '';
            $temp['area'] = $item->area->area ?? '';
            $temp['country'] = $item->country->country ?? '';
            $dayAmountArray = DailyAmount::getDaysAmount($startTime, $endTime, $item->plat_id, $item->account_id, $item->country_id);
            $amount = 0;
            for($i = $days; $i >= 0; $i--){
                $dateTitle = date("Y-m-d", strtotime("$timeMax -$i day")); //日期格式 (2022-01-01)
                if(array_key_exists($dateTitle, $dayAmountArray)){
                    $amount += $dayAmountArray[$dateTitle]; //赋值,当期销量(当天)
                }
            }
            $temp['amount'] = $amount;
            //每日销量
            for($i = $days; $i >= 0; $i--){
                $dateTitle = date("Y-m-d", strtotime("$timeMax -$i day")); //日期格式 (2022-01-01)
                if(array_key_exists($dateTitle, $dayAmountArray)){
                    $temp[$dateTitle] = $dayAmountArray[$dateTitle]; //赋值,当期销量(当天)
                }else{
                    $temp[$dateTitle] = 0;
                }
            }
            $temp['avg3'] = $item->avg3;
            $temp['avg7'] = $item->avg7;
            $temp['avg15'] = $item->avg15;
            $temp['avg30'] = $item->avg30;
            $temp['avg'] = $item->avg;
            $temp['sales_status'] = DailySv::$_salesStatus[$item->sales_status];
            yield $temp;
        }
    }
}
